//
//  YDABaseNavigationController.m
//  PeiZiWangBei
//
//  Created by LK-IOS on 2018/2/28.
//  Copyright © 2018年 LK-IOS. All rights reserved.
//

#import "YDABaseNavigationController.h"
#import "UIViewController+SYANavigationExtion.h"

//全局返回图片
#define kDefaultBackImageName @"nav-all-back"

#pragma mark  MJBWrapNavigationController

@interface MJBWrapNavigationController  : UINavigationController

@end
@implementation MJBWrapNavigationController

- (UIViewController *)popViewControllerAnimated:(BOOL)animated
{
    return [self.navigationController popViewControllerAnimated:animated];
}

- (NSArray<UIViewController *> *)popToRootViewControllerAnimated:(BOOL)animated
{
    return [self.navigationController popToRootViewControllerAnimated:animated];
}

- (NSArray<UIViewController *> *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    YDABaseNavigationController *mj_navigationController = viewController.mj_navigationController;
    NSInteger index = [mj_navigationController.mj_viewControllers indexOfObject:viewController];
    return [self.navigationController popToViewController:mj_navigationController.viewControllers[index] animated:animated];
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    
    viewController.mj_navigationController = (YDABaseNavigationController *)self.navigationController;
    
    viewController.mj_fullScreenPopGestureEnabled = viewController.mj_navigationController.fullScreenPopGestureEnabled;
    
    UIImage *backButtonImage = viewController.mj_navigationController.backButtonImage;
    
    if (!backButtonImage) {
        backButtonImage = [UIImage imageWIthOriginalImage:kDefaultBackImageName];
    }
    
    viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:backButtonImage style:UIBarButtonItemStylePlain target:self action:@selector(didTapBackButton)];
    //告诉哪个界面取消全局滑动返回

    
    [self.navigationController pushViewController:[SYAWrapViewController wrapViewControllerWithViewController:viewController] animated:animated];
}

- (void)didTapBackButton
{
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)popToRootViewController
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
-(void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion
{
    [self.navigationController dismissViewControllerAnimated:flag completion:completion];
    self.viewControllers.firstObject.mj_navigationController=nil;
}

@end

#pragma mark  SYAWrapViewController

static NSValue *mj_tabBarRectValue;


@interface SYAWrapViewController ()


@property (nonatomic, copy) NSString *className;


@end

@implementation SYAWrapViewController

+ (SYAWrapViewController *)wrapViewControllerWithViewController:(UIViewController *)viewController
{
    
    MJBWrapNavigationController *wrapNavController = [[MJBWrapNavigationController alloc] init];
    wrapNavController.viewControllers = @[viewController];
    SYAWrapViewController *wrapViewController = [[SYAWrapViewController alloc] init];
    [wrapViewController.view addSubview:wrapNavController.view];
    [wrapViewController addChildViewController:wrapNavController];
    return wrapViewController;
}


-(NSString *)className
{
    return  NSStringFromClass([self rootViewController].class);
}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    
    if (self.tabBarController && !mj_tabBarRectValue) {
        mj_tabBarRectValue = [NSValue valueWithCGRect:self.tabBarController.tabBar.frame];
    }
    
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    
    if (self.tabBarController && [self rootViewController].hidesBottomBarWhenPushed) {
        self.tabBarController.tabBar.frame = CGRectZero;
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    
    self.tabBarController.tabBar.translucent = YES;
    if (self.tabBarController && !self.tabBarController.tabBar.hidden && mj_tabBarRectValue) {
        self.tabBarController.tabBar.frame = mj_tabBarRectValue.CGRectValue;
    }
}

- (BOOL)mj_fullScreenPopGestureEnabled
{
    return [self rootViewController].mj_fullScreenPopGestureEnabled;
}

- (BOOL)hidesBottomBarWhenPushed
{
    return [self rootViewController].hidesBottomBarWhenPushed;
}

- (UITabBarItem *)tabBarItem
{
    return [self rootViewController].tabBarItem;
}

- (NSString *)title
{
    return [self rootViewController].title;
}

- (UIViewController *)childViewControllerForStatusBarStyle
{
    return [self rootViewController];
}

- (UIViewController *)childViewControllerForStatusBarHidden
{
    return [self rootViewController];
}

- (UIViewController *)rootViewController
{
    MJBWrapNavigationController *wrapNavController = self.childViewControllers.firstObject;
    return wrapNavController.viewControllers.firstObject;
}

@end

#pragma mark YDABaseNavigationController
@interface YDABaseNavigationController ()<UINavigationControllerDelegate, UIGestureRecognizerDelegate>


@property (nonatomic, strong) UIPanGestureRecognizer *popPanGesture;

@property (nonatomic, strong) id popGestureDelegate;

@end

@implementation YDABaseNavigationController

+(void)load
{
    //获取当前导航控制器的UINavigationBar 通过富文本属性修改导航跳上内容的属性

    UINavigationBar *bar = [UINavigationBar appearanceWhenContainedIn:self, nil];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    
    
    //导航栏字体带下及颜色
    dict[NSFontAttributeName] = [UIFont systemFontOfSize:font20ScreenWRate * LKScreenW];
    dict[NSForegroundColorAttributeName] = NumberColor(255, 255, 255);
    bar.titleTextAttributes = dict;
    
    //导航条的背景图
    [bar setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithHexString:@"0E4483"]] forBarMetrics:UIBarMetricsDefault];

}

- (instancetype)initWithRootViewController:(UIViewController *)rootViewController
{
    if (self = [super init]) {
        rootViewController.mj_navigationController = self;
        self.viewControllers = @[[SYAWrapViewController wrapViewControllerWithViewController:rootViewController]];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder]) {
        self.viewControllers.firstObject.mj_navigationController = self;
        self.viewControllers = @[[SYAWrapViewController wrapViewControllerWithViewController:self.viewControllers.firstObject]];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setNavigationBarHidden:YES];
    
    self.delegate = self;
    self.popGestureDelegate = self.interactivePopGestureRecognizer.delegate;
    SEL action = NSSelectorFromString(@"handleNavigationTransition:");
    self.popPanGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self.popGestureDelegate action:action];
    self.popPanGesture.maximumNumberOfTouches = 1;
}


#pragma mark - UINavigationControllerDelegate

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    
    
    BOOL isRootVC = viewController == navigationController.viewControllers.firstObject;
    
    if (viewController.mj_fullScreenPopGestureEnabled) {
        if (isRootVC) {
            [self.view removeGestureRecognizer:self.popPanGesture];
        } else {
            [self.view addGestureRecognizer:self.popPanGesture];
        }
        self.interactivePopGestureRecognizer.delegate = self.popGestureDelegate;
        self.interactivePopGestureRecognizer.enabled = NO;
    } else {
        [self.view removeGestureRecognizer:self.popPanGesture];
        self.interactivePopGestureRecognizer.delegate = self;
        self.interactivePopGestureRecognizer.enabled = !isRootVC;
    }
    //取消左滑手势
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
    {
        
    }
}
#pragma mark - UIGestureRecognizerDelegate
//修复有水平方向滚动的ScrollView时边缘返回手势失效的问题
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return [gestureRecognizer isKindOfClass:UIScreenEdgePanGestureRecognizer.class];
}
#pragma mark - Getter

- (NSArray *)mj_viewControllers
{
    NSMutableArray *viewControllers = [NSMutableArray array];
    for (SYAWrapViewController *wrapViewController in self.viewControllers) {
        [viewControllers addObject:wrapViewController.rootViewController];
    }
    return viewControllers.copy;
}

@end
